package com.blockmeta.bbs.minemodule.activity;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.utils.ToastUtil;
import com.blockmeta.bbs.baselibrary.utils.gson.GsonUtil;
import com.blockmeta.bbs.baselibrary.utils.loading.ProgressBarUtil;
import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.constant.NetConstant;
import com.blockmeta.bbs.businesslibrary.eventbus.MsgSuccess;
import com.blockmeta.bbs.businesslibrary.net.callback.ApiStringCallBack;
import com.blockmeta.bbs.businesslibrary.net.pojo.ErrorPOJO;
import com.blockmeta.bbs.businesslibrary.pojo.UserPhonePOJO;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.minemodule.R;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.MediaType;


public class EditUserInfoActivity extends BaseActivity {
    private Context mContext;
    private View view;
    private TextView tv;
    private EditText et;
    private Button btn;

    @Override
    protected boolean showTitle() {
        return true;
    }

    @Override
    public View setView() {
        mContext = EditUserInfoActivity.this;
        setTitleText("用户名修改");
        view = LayoutInflater.from(mContext).inflate(R.layout.activity_edituserinfo, null);
        initData();
        return view;
    }

    private void initData() {
        tv = (TextView) view.findViewById(R.id.edit_myinfo_tv);
        et = (EditText) view.findViewById(R.id.edit_myinfo_et);
        btn = (Button) view.findViewById(R.id.edit_myinfo_btn);
        tv.setText("用户名");

        initSend();

    }

    private void initSend() {
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(et.getText().toString().trim())) {
                    ToastUtil.show("修改内容不能为空");
                    return;
                }
                sendEditRequest();
            }
        };

        btn.setOnClickListener(onClickListener);

    }

    /**
     * 发送修改用户数据
     * put请求
     */
    private void sendEditRequest() {
        ProgressBarUtil.showProgress(mContext);
        final HashMap<String, Object> map = new HashMap<>();
        final String newName = et.getText().toString().trim();
        map.put("uid", AccountHelper.getUidInt());
        map.put("nickName", newName);
        OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(new Gson().toJson(map))
                .url(NetConstant.USER_INFO_EDIT)
                .build()
                .execute(new ApiStringCallBack(mContext) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        ProgressBarUtil.dismissProgress(mContext);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        ProgressBarUtil.dismissProgress(mContext);
                        if (code == 200) {
                            UserPhonePOJO userPhonePOJO = AccountHelper.getUserPhonePOJO();
                            userPhonePOJO.setNickName(newName);
                            AccountHelper.setUserPhonePOJO(userPhonePOJO);
                            EventBus.getDefault().postSticky(new MsgSuccess("editName", et.getText().toString()));
                            finish();
                        } else {
                            ErrorPOJO errorPOJO = GsonUtil.buildGson().fromJson(response, ErrorPOJO.class);
                            ToastUtil.show(errorPOJO.getMessage());
                        }


                    }
                });

    }
}
